package edu.stu.mall.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import edu.stu.mall.databinding.ItemAttributeValueListBinding
import edu.stu.mall.repository.data.ProductAttributeValue

class ProductAttributeValueAdapter : RecyclerView.Adapter<ProductAttributeValueAdapter.ProductAttributeValueViewHolder>() {

    private var dataList: List<ProductAttributeValue> = mutableListOf()
    fun setData(list: List<ProductAttributeValue>?) {
        if (list != null && list.isNotEmpty()) {
            dataList = list
            notifyDataSetChanged()
        }
    }


    class ProductAttributeValueViewHolder(binding: ItemAttributeValueListBinding) : RecyclerView.ViewHolder(binding.root) {
        var itemBinding: ItemAttributeValueListBinding

        init {
            itemBinding = binding
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductAttributeValueViewHolder {

        return ProductAttributeValueViewHolder(
            ItemAttributeValueListBinding.inflate(
                LayoutInflater.from(parent.context),
                parent, false
            )
        )
    }

    override fun getItemCount(): Int {
        return dataList.size

    }

    override fun onBindViewHolder(holder: ProductAttributeValueViewHolder, position: Int) {
        holder.itemBinding.item = dataList[position]

    }
}